package com.sunhy.accountserver.config;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class AccountTableShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {
        Integer userId = preciseShardingValue.getValue();
        int tableSuffix = userId / 200 + 1;
        String tableName = preciseShardingValue.getLogicTableName() + "_" + tableSuffix;
        if (tableSuffix > collection.size() + 1)
            tableName = preciseShardingValue.getLogicTableName() + "_" + collection.size();
        return tableName;
    }

}
